package com.contract.utils;



import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class CommTools {
	//使用到Algerian字体，系统里没有的话需要安装字体，字体只显示大写，去掉了1,0,i,o几个容易混淆的字符
    public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
    private static Random random = new Random();
	/**
	 * 获取当前日期
	 * @return
	 */
	public static String getDay() {  
		String nextday = "";
		try {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
			Date date=new Date();  
			Calendar calendar = Calendar.getInstance();  
			calendar.setTime(date);  
			date = calendar.getTime();  
			nextday = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nextday;			
    } 
	
	/**
	 * 获取未来几天数据
	 * @param day
	 * @return
	 */
	public static String getNextDay(int day) {  
		String nextday = "";
		try {
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
			Date date=new Date();  
			Calendar calendar = Calendar.getInstance();  
			calendar.setTime(date);  
			calendar.add(Calendar.DAY_OF_MONTH, day);  
			date = calendar.getTime();  
			nextday = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nextday;			
    }
	
	public static Date getDateToString(String time){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  		
		Date date = null;
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {			
			e.printStackTrace();
		}  
		return date;
	}
	public static Date getDateToStrings(String time){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//小写的mm表示的是分钟  		
		Date date = null;
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {			
			e.printStackTrace();
		}  
		return date;
	}
	
	public static String getStringToDate(Date date){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");//小写的mm表示的是分钟  			
		String format = "";
		try {			
			 format = sdf.format(date);
		} catch (Exception e) {			
			e.printStackTrace();
		}  
		return format;
	}
	
	public static String getStringToDateTwo(Date date){
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  			
		String format = "";
		try {			
			 format = sdf.format(date);
		} catch (Exception e) {			
			e.printStackTrace();
		}  
		return format;
	}
	
	/**
	 * MD5加密
	 * @param context
	 * @return
	 * @throws NoSuchAlgorithmException
	 */
    public static String encrypByMd5(String context) throws NoSuchAlgorithmException {  
      
            MessageDigest md = MessageDigest.getInstance("MD5");  
            md.update(context.getBytes());//update处理  
            byte [] encryContext = md.digest();//调用该方法完成计算  
  
            int i;  
            StringBuffer buf = new StringBuffer("");  
            for (int offset = 0; offset < encryContext.length; offset++) {//做相应的转化（十六进制）  
                i = encryContext[offset];  
                if (i < 0) i += 256;  
                if (i < 16) buf.append("0");  
                buf.append(Integer.toHexString(i));  
           }  
           return buf.toString();     
    }  

    /**
     * 使用指定源生成验证码
     * @param verifySize    验证码长度
     * @param sources   验证码字符源
     * @return
     */
    public static String generateVerifyCode(int verifySize){

        int codesLen = VERIFY_CODES.length();
        Random rand = new Random(System.currentTimeMillis());
        StringBuilder verifyCode = new StringBuilder(verifySize);
        for(int i = 0; i < verifySize; i++){
            verifyCode.append(VERIFY_CODES.charAt(rand.nextInt(codesLen-1)));
        }
        return verifyCode.toString();
    }
    
    
    /**
    * 判断某一时间是否在一个区间内
    * 
    * @param sourceTime
    *            时间区间,半闭合,如[10:00-20:00)
    * @param curTime
    *            需要判断的时间 如10:00
    * @return 
    * @throws IllegalArgumentException
    */
   public static boolean isInTime(String startTime,String endTime) {
      
       SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
       try {
    	   String nowTime = sdf.format(new Date());
           long now = sdf.parse(nowTime).getTime();
           long start = sdf.parse(startTime).getTime();
           long end = sdf.parse(endTime).getTime();
           
           if (endTime.equals("00:00")) {
        	   endTime = "24:00";
           }
           if (end < start) {
               if (now >= end && now <= start) {
                   return false;
               } else {
                   return true;
               }
           } 
           else {
               if (now >= start && now <= end) {
                   return true;
               } else {
                   return false;
               }
           }
       } catch (ParseException e) {
           e.printStackTrace();
           return false;
       }

   }
}
